package day_2022_7_to_8.day48;

import java.util.*;

/**
 * @author haomin
 * @date 2022/09/02 18:53
 **/
public class Day48_4_Offer38_Permutation {
    public static void main(String[] args) {
        permutation("abcdefggg");
    }
    public static Set<String> list = new HashSet<>();
    public static String[] permutation(String s) {
        char[] ch = s.toCharArray();
        dfs(ch, new StringBuffer(), new int[ch.length]);
        return list.toArray(new String[0]);
    }
    private static void dfs(char[] ch,StringBuffer ret, int[] flag){
        if(ret.toString().length() == ch.length){
            list.add(ret.toString());
        }
        for(int i = 0; i < ch.length; ++i){
            if(flag[i] == 1) continue;
            ret.append(ch[i]); flag[i] = 1;
            dfs(ch, ret, flag);
            ret.deleteCharAt(ret.length()-1); flag[i] = 0;
        }
    }

}